package uscdb.util.StringUtil;

import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;

public class StringUtil {
	/**
	 * 功能 ： 转换html的特殊符号，防止攻击
	 * 
	 * @param input
	 *            输入的字符串
	 * @return
	 */
	public static String filterHtml(String input) {
		if (input == null) {
			return null;
		}
		if (input.trim().length() == 0) {
			return input;
		}
		input = input.replaceAll("<", "&lt;").replaceAll(">", "&gt;").replaceAll(" ", "&nbsp;").replaceAll("'", "&#39;")
				.replaceAll("\"", "&quot;").replaceAll("\n", "<br>");
		return input;
	}

	/**
	 * 功能：去除字符串里面的sql符号
	 * 
	 * @param input
	 *            输入的字符串
	 * @return
	 */
	public static String filterSql(String input) {
		if (input == null) {
			return null;
		}
		if (input.trim().length() == 0) {
			return input;
		}
		input = input.replaceAll(":", "").replaceAll("[?]", "").replaceAll(",", "").replaceAll(" ", "")
				.replaceAll(" ", "").replaceAll("%", "").replaceAll("[|]", "").replaceAll("&", "").replaceAll("[(]", "")
				.replaceAll("[)]", "");
		return input;
	}

	/**
	 * 功能： 过滤特殊符号
	 * 
	 * @param input
	 *            输入的字符串
	 * @return
	 */
	public static String filterSpecialSymbol(String input) {
		if (input == null) {
			return null;
		}
		if (input.trim().length() == 0) {
			return input;
		}
		input = input.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\(", "").replaceAll("\\)", "")
				.replaceAll("\\+", "").replaceAll("-", "").replaceAll("=", "").replaceAll("\\*", "").replaceAll("&", "")
				.replaceAll("^", "").replaceAll("%", "").replaceAll("!", "").replaceAll("@", "").replaceAll("#", "")
				.replaceAll("$", "").replaceAll("\\\\", "").replaceAll("/", "");
		return input;
	}

	/**
	 * 功能：过滤一切特殊符号，是上述函数功能的汇总
	 * 
	 * @param input
	 *            输入的字符串
	 * @return
	 */
	public static String filterAll(String input) {
		if (input == null) {
			return null;
		}
		if (input.trim().length() == 0) {
			return input;
		}
		input = filterSql(filterHtml(input));
		return input;
	}

	/**
	 * fastjson null值转换为"",日期格式这里想处理成字符串，个人认为按照默认的即可
	 * 
	 * @param object
	 * @param features
	 * @return
	 */
	public static String toJSONString(Object object, SerializerFeature... features) {
		SerializeWriter out = new SerializeWriter();
		String s;
		JSONSerializer serializer = new JSONSerializer(out);
		SerializerFeature arr$[] = features;
		int len$ = arr$.length;
		for (int i$ = 0; i$ < len$; i$++) {
			SerializerFeature feature = arr$[i$];
			serializer.config(feature, true);
		}

		serializer.getValueFilters().add(new ValueFilter() {
			public Object process(Object obj, String s, Object value) {
				if (null != value) {
					// if (value instanceof java.util.Date) {
					// return String.format("%1$tF %1tT", value);
					// }
					return value;
				} else {
					return "";
				}
			}
		});
		serializer.getPropertyFilters().add(new FastjsonFilter());
		serializer.write(object);
		s = out.toString();
		out.close();
		return s;
	}

	/**
	 * 功能：判断字符串是否为空，空为true，非空为false
	 * 
	 * @param string
	 *            消需要判断的字符串
	 * @return
	 */
	public static Boolean isEmpty(String string) {
		if (null == string || "".equals(string.trim()) || string.isEmpty()) {
			return true;
		}
		return false;
	}

	/**
	 * 功能：AI对话，但我认为目前是人工智障，~(>.<)~
	 * 
	 * @param type
	 *            需要反馈语言的场景，目前是 import 场景与 export 场景
	 * @return
	 */
	public static String speakToUser(String type) {
		int num = 0;
		String word = "";
		switch (type) {
		case "import":
			num = (int) (Math.random() * 10) % 3;
			switch (num) {
			case 0:
				word = "别以为我不知道你上传了垃圾，哼~";
				break;
			case 1:
				word = "不是我想要的文件啦！";
				break;
			case 2:
				word = "不如你找管理员确认一下我到底需要什么？亲爱的^.^么么哒(づ￣ 3￣)づ";
				break;
			default:
				word = "The system is broken!";
				break;
			}
			break;

		case "export":
			num = (int) (Math.random() * 10) % 3;
			switch (num) {
			case 0:
				word = "没有文件被修改，所以没压缩文件！";
				break;
			case 1:
				word = "不要试啦，真的没文件~看清楚上面的文字";
				break;
			case 2:
				word = "再试我告诉管理员查你水表哦，哼~";
				break;
			default:
				word = "The system is broken!";
				break;
			}
			break;
		default:
			break;
		}

		return word;
	}

}
